package org.calvados.study.webflux.service.impl;

import org.calvados.study.webflux.domain.User;
import org.calvados.study.webflux.mapper.UserMapper;
import org.calvados.study.webflux.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        Assert.notNull(userMapper, "userMapper can not be null.");
        this.userMapper = userMapper;
    }

    @Override
    public Flux<User> listAllUser() {
        List<User> userList = userMapper.listAllUser();
        return Flux.fromStream(userList.stream()
                .peek(user -> {
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    user.setPassword(null);
                }));
    }
}
